<template>
  <div>
    <el-form
      ref="formRef"
      :model="userInfo"
      :rules="accountRules"
      class="demo-ruleForm"
      :size="formSize"
    >
      <el-form-item label="账号" prop="name">
        <el-input v-model="userInfo.name" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="userInfo.password" type="password" show-password />
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { ElForm } from 'element-plus';
import { reactive, ref, defineExpose } from 'vue';
import { useStore } from 'vuex';
import { accountRules } from '../config';
import hrRequest from '@/service/index';
import localStroage from '@/common/cache';
const store = useStore();
let userInfo = reactive({
  name: '',
  password: localStroage.getItem('password')
});
const formRef = ref<InstanceType<typeof ElForm>>();
// 登录逻辑
// 1.验证账号和密码是否通过
// 2.是否记住了密码
// 3.发起登录请求
// 4.存储登录请求响应发来的token和用户信息
// 5.跳转页面
const loginByAccount = (isRemPassword: boolean) => {
  formRef.value?.validate((valid) => {
    if (valid) {
      if (isRemPassword) {
        localStroage.setItem('password', userInfo.password);
        store.dispatch('accountLogin/accountLoginAction', { ...userInfo });
      }
      console.log('验证通过');
    }
  });
};
defineExpose({
  loginByAccount
});
// export default defineComponent({
//   name: 'loginAccount',
//   setup() {
//     let userInfo = reactive({
//       account: '',
//       password: ''
//     });
//     const formRef = ref<InstanceType<typeof ElForm>>();
//     const loginByAccount = () => {
//       formRef.value?.validate((valid) => {
//         if (valid) {
//           console.log('验证通过');
//         }
//       });
//     };
//     return { userInfo, formRef, loginByAccount, accountRules };
//   }
// });
</script>
<style lang="less" scoped></style>
